package com.dks.util;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

import org.springframework.stereotype.Component;

/**
 * @Author: Ah Nan
 * @Description: 平台工具类
 * @Date: 14:08 2020/4/21
 * @Modificd By:
 * @Param:
 * @return:
 * @throw: 请描述异常信息
 */
@Component
public class QSUnirest {

    public QSUnirest() {
    }

    /**
     * get方式调用-基本通用.
     * @author Ah Nan
     * @Description:
     * @Date: 14:08 2020/4/21
     * @Modificd By:
     * @param apiUrl 完整的URL地址
     * @return java.lang.String
     * @throws UnirestException 请描述异常信息
     */
    public static String unirestGetBase(final String apiUrl) throws UnirestException {
        final HttpResponse<String> response = Unirest.get(apiUrl)
                .header("cache-control", "no-cache")
                .asString();
        return response.getBody();
    }

    /**
     * post方式调用-基本通用.
     * @author Ah Nan
     * @Date: 14:08 2020/4/21
     * @Modificd By:
     * @param apiUrl 完整的URL地址
     * @param paramJson paramJson
     * @return java.lang.String
     * @throws UnirestException 请描述异常信息
     */
    public String unirestPostBase(final String apiUrl, final String paramJson) throws UnirestException {
        final HttpResponse<String> response = Unirest.post(apiUrl)
                .header("content-type", "application/json")
                .header("cache-control", "no-cache")
                .body(paramJson)
                .asString();
        return response.getBody();
    }
}
